package com.nodecollege.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsConfig;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.dao.mapper.UpmsConfigMapper;
import com.nodecollege.cloud.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LC
 * @date 2020/2/7 22:12
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private UpmsConfigMapper upmsConfigMapper;

    /**
     * 查询配置列表
     *
     * @param queryVO
     * @return
     */
    @Override
    public NCResult<UpmsConfig> getConfigList(QueryVO<UpmsConfig> queryVO) {
        checkConfig(queryVO.getData());
        List<UpmsConfig> list = new ArrayList<>();
        Long total = 0L;
        if (NCConstants.INT_NEGATIVE_1.equals(queryVO.getPageSize())) {
            list = upmsConfigMapper.selectListByMap(queryVO.toMap());
            total = NCUtils.isNullOrEmpty(list) ? 0 : Long.parseLong(list.size() + "");
        } else {
            Page page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
            if (queryVO.isSort()) {
                page.setOrderBy(queryVO.getSortKey() + " " + queryVO.getSortDirection());
            }
            list = upmsConfigMapper.selectListByMap(queryVO.toMap());
            total = page.getTotal();
        }
        return NCResult.ok(list, total);
    }

    /**
     * 查询单个配置信息
     *
     * @param config
     * @return
     */
    @Override
    public UpmsConfig getOne(UpmsConfig config) {
        checkConfig(config);
        List<UpmsConfig> list = upmsConfigMapper.selectListByMap(new QueryVO<>(config).toMap());
        NCUtils.nullOrEmptyThrow(list, new UpmsException("", "该配置文件不存在！"));
        if (list.size() > 1) {
            throw new UpmsException("", "查出多条数据，请检查参数是否正确！");
        }
        return list.get(0);
    }

    /**
     * 添加配置信息
     *
     * @param config
     */
    @Override
    public void addConfig(UpmsConfig config) {
        NCUtils.nullOrEmptyThrow(config.getConfigName(), new UpmsException("", "配置名称必填！"));
        NCUtils.nullOrEmptyThrow(config.getConfigCode(), new UpmsException("", "配置代码必填！"));
        NCUtils.nullOrEmptyThrow(config.getConfigValue(), new UpmsException("", "配置值必填！"));
        checkConfig(config);
        UpmsConfig query = new UpmsConfig();
        query.setConfigType(config.getConfigType());
        query.setTenantId(config.getTenantId());
        query.setConfigCode(config.getConfigCode());
        List<UpmsConfig> exList = upmsConfigMapper.selectListByMap(new QueryVO<>(query).toMap());
        NCUtils.notNullOrNotEmptyThrow(exList, new UpmsException("", "该配置代码已使用！"));

        query.setConfigCode(null);
        query.setConfigName(config.getConfigName());
        exList = upmsConfigMapper.selectListByMap(new QueryVO<>(query).toMap());
        NCUtils.notNullOrNotEmptyThrow(exList, new UpmsException("", "该配置名称已使用！"));
        config.setState(NCConstants.INT_1);
        upmsConfigMapper.insertSelective(config);
    }

    /**
     * 更新配置信息
     *
     * @param config
     */
    @Override
    public void updateConfig(UpmsConfig config) {
        checkConfig(config);
        NCUtils.nullOrEmptyThrow(config.getConfigId(), new UpmsException("", "配置id必填！"));

        UpmsConfig ex = upmsConfigMapper.selectByPrimaryKey(config.getConfigId());
        NCUtils.nullOrEmptyThrow(ex, new UpmsException("", "该配置不存在！"));
        if (config.getTenantId() != null && !config.getTenantId().equals(ex.getTenantId())) {
            throw new UpmsException("", "该配置不存在！");
        }
        if (ex.getState() == NCConstants.INT_0) {
            // 状态为0的数据不能修改配置名称和配置代码
            config.setConfigName(null);
            config.setConfigCode(null);
        }

        UpmsConfig query = new UpmsConfig();
        query.setConfigType(config.getConfigType());
        query.setTenantId(config.getTenantId());
        List<UpmsConfig> exList = new ArrayList<>();
        if (NCUtils.isNotNullOrNotEmpty(config.getConfigCode())) {
            query.setConfigCode(config.getConfigCode());
            exList = upmsConfigMapper.selectListByMap(new QueryVO<>(query).toMap());
            if (exList.size() == 1 && !exList.get(0).getConfigId().equals(config.getConfigId())) {
                throw new UpmsException("", "该配置代码已使用！");
            }
        }

        if (NCUtils.isNotNullOrNotEmpty(config.getConfigName())) {
            query.setConfigCode(null);
            query.setConfigName(config.getConfigName());
            exList = upmsConfigMapper.selectListByMap(new QueryVO<>(query).toMap());
            NCUtils.notNullOrNotEmptyThrow(exList, new UpmsException("", "该配置名称已使用！"));
        }
        config.setState(null);
        config.setConfigType(null);
        upmsConfigMapper.updateByPrimaryKeySelective(config);
    }

    /**
     * 删除配置信息
     *
     * @param config
     */
    @Override
    public void delConfig(UpmsConfig config) {
        checkConfig(config);
        NCUtils.nullOrEmptyThrow(config.getConfigId(), new UpmsException("", "配置id必填！"));

        UpmsConfig ex = upmsConfigMapper.selectByPrimaryKey(config.getConfigId());
        NCUtils.nullOrEmptyThrow(ex, new UpmsException("", "该配置不存在！"));
        if (config.getTenantId() != null && !config.getTenantId().equals(ex.getTenantId())) {
            throw new UpmsException("", "该配置不存在！");
        }
        if (ex.getState() == NCConstants.INT_0) {
            throw new UpmsException("", "该配置不能删除！");
        }
        upmsConfigMapper.deleteByPrimaryKey(config.getConfigId());
    }

    private void checkConfig(UpmsConfig config) {
        NCUtils.nullOrEmptyThrow(config.getConfigType(), new UpmsException("", "配置类型必填！"));
        if (config.getConfigType() == NCConstants.INT_1 && config.getTenantId() == null) {
            throw new UpmsException("", "租户id必填！");
        }
    }
}
